/*

 MBtvServer
 
 Copyright (c) 2007 Mary Jane Soft - Marlon J. Manrique
        
 http://mjs.darkgreenmedia.com
 http://marlonj.darkgreenmedia.com
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 $Id: MBtvServer.pde 340 2007-12-28 00:14:42Z marlonj $
        
*/

/**
 * This server allows the transmission of video over bluetooth,
 * the videos are store into the data directory and two files 
 * allow the configuration of channels of the server and create 
 * a lis of files for each client 
 *
 * Changes :
 * 
 * 0.1 (December - 2007) Initial Release  
 *
 * @author Marlon J. Manrique
 */

import mjs.processing.mobile.mbt.*;
import mjs.processing.mobile.mmediaserver.*;

// Library objects 
MBt mBt;
MMediaServer mMediaServer;

// Server messages
String message;

/*
 * Create the sketch, init font settings and objects 
 */
void setup()
{
  // Init sketch 
  //initMobile();
  initProcessing();
  
  // Set font settings     
  textAlign(CENTER);
  
  // Set initial message 
  message = "Press Any Key to Start Server";
}

/*void initMobile()
{
  // Set font settings 
  textFont(loadFont());
  
  // Create library objects 
  mBt = new MBt(this);
  mMediaServer = new MMediaServer(this,mBt);  
}*/

void initProcessing()
{
  // Set font settings 
  textFont(createFont("monospaced",12));
  
  // Create library objects 
  mBt = new MBtP(this);
  mMediaServer = new MMediaServerP(this,mBt);
}

/*
 * Draw the status of the server 
 */
void draw()
{
  // Clear the screen 
  background(0);
  
  // Draw the message if any 
  if(message != null)
    text(message,width/2,height/2);
}

/*
 * If a key is pressed, start the server 
 */
void keyPressed()
{
  mMediaServer.start();
}

/*
 * Check for library events 
 *
 * @param library Library that fires the event 
 * @param event event identifier 
 * @param data Data fire with the event 
 */
void libraryEvent(Object library, int event, Object data)
{
  // Check for mediaserver events 
  if(library == mMediaServer)
  {
    switch(event)
    {
      // If any error, show at the screen 
      case MMediaServer.EVENT_ERROR : 
        message = (String) data;
        break;

      // The server is running 
      case MMediaServer.EVENT_SERVER_STARTED : 
        message = "Server Started";
        break;        

      // The server is not running 
      case MMediaServer.EVENT_SERVER_STOPPED : 
        message = "Server Stopped";
        break;        
        
      // A client is connected 
      case MMediaServer.EVENT_CLIENT_AVAILABLE : 
        message = "Client Connected";
        break;        

      // A client disconnected 
      case MMediaServer.EVENT_CLIENT_DISPATCHED : 
        message = "Client Dispatched";
        break;        
    }
  }
}

/*
 * Destroy the application 
 */
void destroy()
{
  // Stop the server 
  mMediaServer.stop();
}

void exit()
{
  destroy();
  super.exit();
}
